package com.jingbit.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CustomInputDialog extends CustomBaseDialog {

    private Context context;
    private Button yes;//确定按钮
    private Button no;//取消按钮
    private TextView titleView;
    private EditText editText;

    private IOkayClickListener okayClickListener;//确定按钮被点击了的监听器
    private ICancelClickListener cancelClickListener;//取消按钮被点击了的监听器

    public CustomInputDialog(Context context) {
        super(context, R.style.CustomDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);
        //初始化界面控件
        yes = findViewById(R.id.btn_okay);
        no = findViewById(R.id.btn_cancel);
        titleView = findViewById(R.id.tv_title);
        editText = findViewById(R.id.et_input);

        //设置确定按钮被点击后，向外界提供监听
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (okayClickListener != null) {
                    if (editText.getText() == null || editText.getText().equals("")) {
                        Toast.makeText(context, editText.getHint(), Toast.LENGTH_LONG).show();
                        return;
                    }
                    okayClickListener.onOkay(editText.getText().toString());
                    editText.setText("");
                    dismiss();
                }
            }
        });

        //设置取消按钮被点击后，向外界提供监听
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (cancelClickListener != null) {
                    cancelClickListener.onCancel();
                }
            }
        });
    }

    public void show(String title, String etHint, String okayText, String cancel, IOkayClickListener okayClickListener, ICancelClickListener cancelClickListener) {
        this.okayClickListener = okayClickListener;
        this.cancelClickListener = cancelClickListener;
        showDialog(Gravity.CENTER, R.style.AnimCenter);
        if (!TextUtils.isEmpty(title)) {
            titleView.setText(title);
        }
        if (!TextUtils.isEmpty(etHint)) {
            editText.setHint(etHint);
        }
        if (!TextUtils.isEmpty(okayText)) {
            yes.setText(okayText);
        }
        if (!TextUtils.isEmpty(cancel)) {
            no.setText(cancel);
        }
        yes.setVisibility(okayClickListener == null ? View.GONE : View.VISIBLE);
        no.setVisibility(cancelClickListener == null ? View.GONE : View.VISIBLE);

    }

    public interface IOkayClickListener {
        public void onOkay(String input);
    }

    public interface ICancelClickListener {
        public void onCancel();
    }
}